/**
 * 检查坐骑相关表结构脚本
 */

const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: '43.143.253.188',
  user: 'nextjs',
  password: 'yEAiRE2jcxTyRBy5',
  database: 'nextjs',
  charset: 'utf8mb4'
};

async function checkMountTables () {
  let connection;

  try {
    console.log('🔌 连接数据库...');
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功');

    // 检查 mount_base 表结构
    console.log('📊 检查 mount_base 表结构:');
    const [baseColumns] = await connection.execute('DESCRIBE mount_base');
    console.log('字段名\t\t类型\t\t允许空值\t键\t默认值\t\t额外');
    console.log('----\t\t----\t\t--------\t--\t------\t\t----');
    baseColumns.forEach(col => {
      console.log(`${col.Field}\t\t${col.Type}\t\t${col.Null}\t\t${col.Key}\t${col.Default}\t\t${col.Extra}`);
    });

    // 检查 character_mounts 表结构
    console.log('\n📊 检查 character_mounts 表结构:');
    const [mountColumns] = await connection.execute('DESCRIBE character_mounts');
    console.log('字段名\t\t类型\t\t允许空值\t键\t默认值\t\t额外');
    console.log('----\t\t----\t\t--------\t--\t------\t\t----');
    mountColumns.forEach(col => {
      console.log(`${col.Field}\t\t${col.Type}\t\t${col.Null}\t\t${col.Key}\t${col.Default}\t\t${col.Extra}`);
    });

    // 查看 mount_base 表数据
    console.log('\n📊 mount_base 表数据:');
    const [baseData] = await connection.execute('SELECT * FROM mount_base LIMIT 5');
    console.log('ID\t名称\t\t初始攻击\t最大攻击\t初始速度\t最大速度');
    console.log('--\t----\t\t--------\t--------\t--------\t--------');
    baseData.forEach(row => {
      console.log(`${row.id}\t${row.name || 'N/A'}\t\t${row.apIni}\t\t${row.ap}\t\t${row.spIni}\t\t${row.sp}`);
    });

    // 查看 character_mounts 表数据
    console.log('\n📊 character_mounts 表数据:');
    const [mountData] = await connection.execute('SELECT * FROM character_mounts WHERE characterId = 2001 LIMIT 5');
    console.log('ID\t角色ID\t名称\t\t等级\t转职\t当前攻击\t当前速度');
    console.log('--\t----\t----\t\t--\t--\t--------\t--------');
    mountData.forEach(row => {
      console.log(`${row.id}\t${row.characterId}\t${row.name || 'N/A'}\t\t${row.level}\t${row.transLevel}\t${row.currentAp}\t\t${row.currentSp}`);
    });

  } catch (error) {
    console.error('❌ 检查失败:', error.message);
    throw error;
  } finally {
    if (connection) {
      await connection.end();
      console.log('🔌 数据库连接已关闭');
    }
  }
}

// 运行脚本
if (require.main === module) {
  checkMountTables().catch((error) => {
    console.error('💥 脚本执行失败:', error.message);
    process.exit(1);
  });
}

module.exports = { checkMountTables };
